
package cn.com.dhc.danlu.activity.home.wrap;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.SerializeCache;
import cn.com.dhc.danlu.widget.adapter.CategorySpinnerListViewAdapter;

/**
 * @author wuhaiyang
 */
public class PopManager {

    private PopupWindow pop;
    private String[] mCodes = new String[] {
            CommonConst.IntentKey.WINE_CODE,
            CommonConst.IntentKey.RED_WINE_CODE, CommonConst.IntentKey.TEA_CODE,
            CommonConst.IntentKey.FOREIGN_CODE, CommonConst.IntentKey.BEER_CODE,
            CommonConst.IntentKey.DRINKS_CODE};  // 品类优化前DRINKS_CODE为"其它"的分类码
    public PopManager(Context context) {
        super ();
        initPop (context);
    }

    private void initPop (final Context context) {
        final LayoutInflater inflater = LayoutInflater.from (context);
        final View view = inflater.inflate(R.layout.layout_spinner_item, null);
        final GridView listview = (GridView) view.findViewById (R.id.formcustomspinner_list);
        List<String> list = Arrays.asList(context.getResources().getStringArray(R.array.wine_type));
        final ArrayList<String> datas = new ArrayList<>();
        datas.addAll(list);
        CategorySpinnerListViewAdapter adapters = new CategorySpinnerListViewAdapter (context, datas);
        listview.setSelector (new ColorDrawable (Color.TRANSPARENT));
        listview.setAdapter (adapters);
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismissPop();
                String code = mCodes[position];
                String[] array = view.getContext().getResources().getStringArray(R.array.wine_type);
                CategoryEvent event = new CategoryEvent(code,array[position]);
                EventBus.getDefault().post (event);
                setDefaultSection(position);
                SerializeCache.getInstance().saveCategoryIndex(position);
            }
        });
        pop = new PopupWindow (view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
        // 在PopupWindow里面就加上下面代码，让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode (PopupWindow.INPUT_METHOD_NEEDED);
        pop.setAnimationStyle(R.style.popanim_category_style);
        pop.setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // 点击空白处时，隐藏掉pop窗口
        pop.setFocusable (true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setOutsideTouchable(true);
        //处理返回按键监听事件
        pop.getContentView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && isShowing()) {
                    dismissPop();
                    return true;
                }
                return false;
            }
        });
    }

    public void setDefaultSection (int defaultPosition) {
        View contentView = pop.getContentView ();
        GridView gridView = (GridView) contentView.findViewById (R.id.formcustomspinner_list);
        if (null != gridView.getAdapter () && gridView.getAdapter () instanceof CategorySpinnerListViewAdapter) {
            CategorySpinnerListViewAdapter adapter = (CategorySpinnerListViewAdapter) gridView.getAdapter ();
            adapter.setDefaultSelectedPos (defaultPosition);
            adapter.notifyDataSetChanged ();
        }
    }

    public boolean isShowing () {
        return pop.isShowing ();
    }

    public void showPop (View view) {
        pop.showAsDropDown (view);
    }
    public void showPopIndex(View view,int pos){
        showPop(view);
        setDefaultSection(pos);
    }


    public void dismissPop () {
        pop.dismiss ();
    }




}
